package org.fangzz.cameramgr.cameramonitor.service.impl;


import org.fangzz.cameramgr.cameramonitor.dto.AreaSummary;
import org.fangzz.cameramgr.cameramonitor.dto.CitySummary;
import org.fangzz.cameramgr.cameramonitor.model.Area;
import org.fangzz.cameramgr.cameramonitor.model.City;
import org.fangzz.cameramgr.cameramonitor.repository.AreaRepository;
import org.fangzz.cameramgr.cameramonitor.repository.CityRepository;
import org.fangzz.cameramgr.cameramonitor.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    AreaRepository areaRepository;

    @Autowired
    CityRepository cityRepository;

    @Override
    public List<Area> findByCityCode(String cityCode) {
        return areaRepository.findByCityCode(cityCode);
    }


    @Override
    public Map<String,List<AreaSummary>> findByProvinceCode(String provinceCode){
        Map<String,List<AreaSummary>> areas = new HashMap<>();
        List<List<AreaSummary>> areaOfCity = new ArrayList<>();
        System.out.println(provinceCode);
        List<City> cityList = cityRepository.findByProvinceCode(provinceCode);
        for (int i = 0; i < cityList.size(); i ++){
            String cityCode = cityList.get(i).getCode();
            List<Area> areaList = areaRepository.findByCityCode(cityCode);
            areas.put(cityList.get(i).getName(),AreaSummary.convert(areaList));
        }
        return  areas;
    };

}
